package main.java.game.command;

import main.java.game.Battle;
import main.java.game.Dice;
import main.java.game.DiceImpl;
import main.java.game.combatant.Combatant;
import main.java.game.combatant.Monster;

/**
 * @author  ILDiNHo
 */
public class AttackHero implements BattleCommand{
	/**
	 * @uml.property  name="battle"
	 * @uml.associationEnd  
	 */
	private Battle battle;
	/**
	 * @uml.property  name="hero"
	 * @uml.associationEnd  
	 */
	private Combatant hero;
	/**
	 * @uml.property  name="monster"
	 * @uml.associationEnd  
	 */
	private Monster monster;
	private Integer diceRoll;

	public AttackHero(Battle b){
		this.battle = b;
		this.hero=battle.getHero();
		this.monster=battle.getMonster();
		Dice dice= new DiceImpl();
		diceRoll=dice.roll();
	}
	public void execute() {
		System.out.println("The dice roll was: "+diceRoll); 
		if(compareAttackMonsterDefenseHero()>0){
			substractHeroLife();
			hero.restaureDefense();
			System.out.println("The monster " + getMonster().getName() + 
					" inflicted damage on your hero: " + battle.getHero().getName() 
					+ " and his life points are now: "+battle.getHero().getLifepoints()+"\n");
		}
		else{
			substractHeroDefense();
			Integer damage= diceRoll+monster.getAttack();
			if(damage<0){
				damage = 0;
			}
			System.out.println("The monster " + getMonster().getName() + 
					" reduced the defense of your hero: " 
					+ getHero().getName()+" "+ damage+" points, and his defense is now: "
					+getHero().getDefense() + "\n");
		}
	}
	/**
	 * @return
	 * @uml.property  name="hero"
	 */
	public Combatant getHero(){
		return hero;
	}
	/**
	 * @return
	 * @uml.property  name="monster"
	 */
	public Monster getMonster(){
		return monster;
	}
	//pongo el compare a public para el TEST
	public Integer compareAttackMonsterDefenseHero(){
		return ((Integer)(this.getMonster().getAttack()+diceRoll)).compareTo(this.getHero().getDefense());
	}
	//pongo el substrac a public para el TEST
	public void substractHeroLife(){
		if(this.getMonster().getName().equals("Basilisk")){
			this.getHero().setLifepoints(0);
		}
		this.getHero().setLifepoints(this.getHero().getLifepoints()-1);
	}
	//pongo el substrac a public para el TEST
	public void substractHeroDefense(){
		
		if(diceRoll + monster.getAttack()>=0)
				hero.setDefense(hero.getDefense()-(diceRoll+monster.getAttack()));
	
	}
	public Battle getAssociatedBattle(){
		return battle;
	}
	
	public Integer getRoll(){
		return diceRoll;
	}
		

}
